﻿param($eng,$spaip,$spbip,[string]$scope,$username,$password,$interval,$URL)

if(!$interval) { $interval = 1 } #default monitoring interval is 5 minutes per vCops recommendations
if(!$spaip -or !$spbip) { write "need both -spaip and -spbip and authentication parameters";exit; }
if(!$URL) { write "Need VCOPs HTTPPostAdapter URL"; exit;}
if($eng) { $engmode = "-eng $eng" }
$auth = "-username $username -password $password -scope $scope"
$path = (Split-Path -parent $MyInvocation.MyCommand.Definition)

$global:textMetrics = ""

function isNumeric ($x) {
    try {
        0 + $x | Out-Null
        return $true
    } catch {
        return $false
    }
}
function sendPost($text){
	$parameters = @"
$text
"@
	$http_request = New-Object -ComObject Msxml2.XMLHTTP
	$http_request.open('POST', $URL, $false)
	$http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	$http_request.setRequestHeader("Content-length", $parameters.length)
	$http_request.setRequestHeader("Connection", "close")
	$http_request.send($parameters)
	#$http_request.statusText
	#$http_request.responseText
}
function runDataCollection {
    Write-Progress -Activity "VNX to vCops Enterprise Statistics Posting" -CurrentOperation "Array Data Post" -Status "Collecting Data from Array" -PercentComplete 1
	get-job | stop-job 
    get-job | remove-job -force
    $job1 = {
                set-location $args[0];
                $cmd = "&'.\get_naviseccli.ps1' -spip "+$args[1]+" "+$args[2]+" -get sp -getpkey 'Storage Processor'"; invoke-expression $cmd
                }    
    $job2 = {
                set-location $args[0];
                $cmd = "&'.\get_naviseccli.ps1' -spip "+$args[1]+" "+$args[2]+" -get sp -getpkey 'Storage Processor'"; invoke-expression $cmd
                }
    $job3 = {
                set-location $args[0];
                $cmd = "&'.\get_naviseccli.ps1' -spip "+$args[1]+" "+$args[2]+" -get lun -getpkey 'name' "+$args[3]; invoke-expression $cmd
                }
    $job4 = {
                set-location $args[0];
                $cmd = "&'.\get_naviseccli.ps1' -spip "+$args[1]+" "+$args[2]+" -get cache "+$args[3]; invoke-expression $cmd
                }				
    start-job -argumentlist $path, $spaip, $auth, $engmode -name job1 $job1 | out-null
    start-job -argumentlist $path, $spbip, $auth, $engmode -name job2 $job2 | out-null
    start-job -argumentlist $path, $spaip, $auth, $engmode -name job3 $job3 | out-null
    start-job -argumentlist $path, $spaip, $auth, $engmode -name job4 $job4 | out-null

    $count=1
    do {
    	$count++
    	sleep 1
    }
    while ((get-job | where {$_.State -ne 'Completed'}) -and ($count -lt 40))
    #write "slept $count seconds"
    if ($count -eq 40) {
        get-job | where {$_.State -ne 'Completed'} | stop-job
        write "quit becuase it took too long"
        exit
    }
	$output = @()
    $output = get-job | receive-job
    $stats = @{}
    $output | %{ $stats += $_ }
	return $stats
}
function AddStat($statName,$statValue) {
	$date = [math]::round(([decimal]((get-date).ToUniversalTime() | Get-Date -UFormat "%s")*1000),0)
	$global:textMetrics = $global:textMetrics + @"
$statName,0,NoValue,$date,$statValue

"@
}
function AddStatHeader($sectionName,$resourceKindKey,$resourceDescription) {
	$global:textMetrics += @"
"$sectionName",$resourceKindKey,$resourceDescription,$interval

"@
}

while ($true) {
	$allstats = runDataCollection
	#For each of the resources in the stats set (SPs & LUNs)
 	Write-Progress -CurrentOperation "Array Data Post" -Activity "VNX to vCops Enterprise Statistics Posting" -Status "Processing Array Data" -PercentComplete 2
	$count = 0
	foreach ($resource in $allstats.Keys) {
	$resourceName = $resource
	$resourceValue = $allstats[$resourceName] #Pull out the key name and its values (another hashtable)
	$global:textMetrics = ""
	AddStatHeader $resourceName "VNXBlock" "VNXBlock Stats"
	foreach ($stat in $resourceValue.Keys) {
		$statName = $stat
		$statValue = $resourceValue[$statName]
		if (isNumeric($statValue)) {AddStat $statName $statValue}
	}
	$percentComplete = ($count++ / $allstats.Count) * 100
	Write-Progress -CurrentOperation "Array Data Post" -Activity "VNX to vCops Enterprise Statistics Posting" -Status "Posting Array Data" -PercentComplete $percentComplete
	sendPost $global:textMetrics 
}
	Write-Progress -CurrentOperation "Array Data Post" -Activity "VNX to vCops Enterprise Statistics Posting" -Status "Processing Array Data" -PercentComplete 100 -Completed
	Write-Host (Get-Date) ": Post Completed - Sleeping $interval minutes before next post"
	sleep ($interval * 60)
}